
package org.charles.android.easlookup;

import org.charles.android.easlookup.R;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class EasLookupActivity extends Activity implements OnClickListener {
    private static final String AUTHORITY = "com.android.exchange.directory.provider";
    private static final String GAL_FILTER = "/data/emails/filter/";
    private static final String GAL_CONTACTS_AVAILABILITY_FILTER = "/contacts/availability/filter/";
//    private static final String ACCOUNT_NAME = "account_name=28850443%40eas.apac.extranet.sonyericsson.com";
    private static final String ACCOUNT_NAME = "account_name=charles%40192.168.1.100";
    private static final String ACCOUNT_TYPE_AND_LIMITS = "account_type=com.android.exchange&limit=15";
    private EditText inpName;

    private TextView txtResult;

    private Button btnSubmit;
    private Button btnQueryAvailibility;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnSubmit = (Button)findViewById(R.id.submit);
        inpName = (EditText)findViewById(R.id.name);
        txtResult = (TextView)findViewById(R.id.result);
        btnQueryAvailibility = (Button)findViewById(R.id.query_availibility);

        btnSubmit.setOnClickListener(this);

        btnQueryAvailibility.setOnClickListener(this);
    }

    protected void searchForAvailability(final String queryString) {
        // TODO Auto-generated method stub
        new AsyncTask<Void, Void, Cursor>() {
            @Override
            protected Cursor doInBackground(Void... params) {
                StringBuffer sb = new StringBuffer();
                sb.append("content://" + AUTHORITY + GAL_FILTER);
                sb.append(queryString);
                sb.append("?" + ACCOUNT_NAME + "&" + ACCOUNT_TYPE_AND_LIMITS);
                sb.append("&");
                sb.append("mypara=charles");
                Uri uri = Uri.parse(sb.toString());
                ContentResolver cr = getContentResolver();
                String[] projection = {
                        "display_name", "data1", "data2", "data3", "contact_id", "_id",
                        "photo_thumb_uri"
                };
                String selection = null;
                String[] selectionArgs = null;
                String sortOrder = null;
                Cursor cursor = cr.query(uri, projection, selection, selectionArgs, sortOrder);
                return cursor;
            }

            protected void onPostExecute(Cursor cursor) {
                ArrayList<String> name = new ArrayList<String>();
                if (cursor != null) {
                    while (cursor.moveToNext()) {
                        name.add(cursor.getString(0));
                    }
                    cursor.close();
                }
                if (name.size() > 0) {
                    StringBuffer result = new StringBuffer();
                    for (String s : name) {
                        result.append(s + "\n");
                    }
                    txtResult.setText(result.toString());
                } else {
                    txtResult.setText("No contact found");
                }
            };
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

    private void log(String string) {
        // TODO Auto-generated method stub
        Log.i("Charles_TAG", string);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String name = inpName.getText().toString();
        if (name != null && name.length() > 2) {
            if ( v == btnSubmit) {
                searchForAvailability(name);
            } else if (v == btnQueryAvailibility) {
                queryAvailibility(name);
            }
        } else {
            txtResult.setText("Please input at least 3 characters");
        }
    }

    private void queryAvailibility(final String queryString) {
        // TODO Auto-generated method stub
        new AsyncTask<Void, Void, Cursor>() {
            @Override
            protected Cursor doInBackground(Void... params) {
                // TODO Auto-generated method stub
                StringBuffer sb = new StringBuffer();
                sb.append("content://" + AUTHORITY + GAL_CONTACTS_AVAILABILITY_FILTER);
                sb.append(queryString);
                sb.append("?" + ACCOUNT_NAME + "&" + ACCOUNT_TYPE_AND_LIMITS);
                sb.append("&");
                sb.append("mypara=charles");
                Uri uri = Uri.parse(sb.toString());
                ContentResolver cr = getContentResolver();
                String[] projection = {
                        "display_name", "data1", "data2", "data3", "contact_id", "_id",
                        "photo_thumb_uri"
                };
                String selection = null;
                String[] selectionArgs = null;
                String sortOrder = null;
                Cursor cursor = cr.query(uri, projection, selection, selectionArgs, sortOrder);
                return null;
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }
}
